import 'dart:io';

import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/widgets/icon_text_button.dart';

import 'picture_ashing_controller.dart';

class PictureAshingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<PictureAshingController>(
        init: PictureAshingController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(title: Text('图片灰化')),
              body: Column(
                children: [
                  _ashingFromDart(controller),
                  _ashingFromRust(controller)
                ],
              ));
        });
  }

  _ashingFromDart(PictureAshingController controller) {
    return Container(
      margin: EdgeInsets.only(left: 15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            'dart',
            style: TextStyle(
                color: Colors.deepPurpleAccent,
                fontWeight: FontWeight.bold,
                fontSize: 21),
          ),
          controller.imagePath.value.isEmpty
              ? InkWell(
                  onTap: () {
                    controller.choosePicture();
                  },
                  child: Container(
                      width: windowWidth * 0.25,
                      height: windowHeight * 0.2,
                      margin:
                          EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                      decoration: BoxDecoration(
                          border: Border.all(
                              width: 1, color: Colors.deepPurpleAccent)),
                      alignment: Alignment.center,
                      child: IconTextButton(onPressed: () {
                        controller.choosePicture();
                      }, Icon(Icons.photo), Text('选择图片'))))
              : Container(
                  margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                  child: Stack(
                    alignment: Alignment.topRight,
                    children: [
                      Image.file(File(controller.imagePath.value),
                          width: windowWidth * 0.25),
                      IconButton(
                          onPressed: () {
                            controller.delete();
                          },
                          icon: Icon(
                            Icons.clear,
                            color: Colors.red,
                          ))
                    ],
                  )),
          IconButton(
              padding: EdgeInsets.all(5),
              onPressed: () {
                controller.ashingFromDart();
              },
              icon: Icon(Icons.arrow_forward_rounded)),
          controller.outputPath.value.isNotEmpty
              ? Container(
                  margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                  child: Row(children: [
                    Image.file(File(controller.outputPath.value),
                        width: windowWidth * 0.25),
                    SizedBox(
                      width: 15,
                    ),
                    Text(
                      '耗时：' + controller.time1.value.toString() + 'ms',
                      style: TextStyle(color: Colors.green),
                    )
                  ]))
              : Container()
        ],
      ),
    );
  }

  _ashingFromRust(PictureAshingController controller) {
    return Container(
      margin: EdgeInsets.only(left: 15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            'rust',
            style: TextStyle(
                color: Colors.deepPurpleAccent,
                fontWeight: FontWeight.bold,
                fontSize: 21),
          ),
          controller.rustImagePath.value.isEmpty
              ? InkWell(
                  onTap: () {
                    controller.choosePicture(type: 1);
                  },
                  child: Container(
                      width: windowWidth * 0.25,
                      height: windowHeight * 0.2,
                      margin:
                          EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                      decoration: BoxDecoration(
                          border: Border.all(
                              width: 1, color: Colors.deepPurpleAccent)),
                      alignment: Alignment.center,
                      child: IconTextButton(onPressed: () {
                        controller.choosePicture(type: 1);
                      }, Icon(Icons.photo), Text('选择图片'))))
              : Container(
                  margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                  child: Stack(alignment: Alignment.topRight, children: [
                    Image.file(File(controller.rustImagePath.value),
                        width: windowWidth * 0.25),
                    IconButton(
                        padding: EdgeInsets.all(5),
                        onPressed: () {
                          controller.delete(type: 1);
                        },
                        icon: Icon(
                          Icons.clear,
                          color: Colors.red,
                        ))
                  ])),
          IconButton(
              onPressed: () {
                controller.ashingFromRust();
              },
              icon: Icon(Icons.arrow_forward_rounded)),
          Offstage(
              offstage: controller.rustOutputPath.value.isEmpty,
              child: Container(
                  margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                  child: Row(
                    children: [
                      Image.file(File(controller.rustOutputPath.value),
                          width: windowWidth * 0.25),
                      SizedBox(
                        width: 15,
                      ),
                      Text(
                        '耗时：' + controller.time2.value.toString() + 'ms',
                        style: TextStyle(color: Colors.green),
                      )
                    ],
                  )))
        ],
      ),
    );
  }
}
